package com.googlecode.animator.controller
{
	
	import com.googlecode.animator.data.Notifications;
	import com.googlecode.animator.data.cameras.ActualViewCameraVO;
	import com.googlecode.animator.data.markers.HideMarkerVO;
	import com.googlecode.animator.data.objects3d.CameraProxyVO;
	import com.googlecode.animator.data.objects3d.Object3DVO;
	import com.googlecode.animator.data.objects3d.TargetProxyVO;
	
	import flash.utils.Dictionary;
	
	import org.papervision3d.core.math.Number3D;
	import org.puremvc.as3.multicore.interfaces.INotification;
	
	/**
	 * Carries out PureMVC application startup. 
	 * @author Martin
	 */
	public class PlayCommand extends BaseCommand
	{
		/**
		 * Execute the command.
		 */		
		override public function execute(notification:INotification):void
		{
			var time:int = notification.getBody() as int;
			var objects3D:Dictionary = settings.getObjects3D();
			var camera:ActualViewCameraVO = settings.mainCamera;
			

			for each(var VO:Object3DVO in objects3D)
			{
				
				var tweens:Array = ((VO as Object3DVO).clone() as Object3DVO).tweens;
				var currentTween:Number3D;
				
				if(tweens.length >= time)
				{
					currentTween = tweens[time];
						

				}else
				{
					currentTween = tweens[tweens.length-1];
				}
				
				if(VO is CameraProxyVO && currentTween != null)
				{
					camera.x = currentTween.x;
					camera.y = currentTween.y;
					camera.z = currentTween.z;
					
					continue;
				}
				
				if(VO is TargetProxyVO && currentTween != null)
				{
					camera.target.x = currentTween.x;
					camera.target.y = currentTween.y;
					camera.target.z = currentTween.z;
					
					continue;
				}
				
				if(VO is TargetProxyVO || VO is CameraProxyVO)
				{
					continue;
				}
				
				if(currentTween != null)
				{
					settings.scene.getChildByName(VO.name).x = currentTween.x;
					settings.scene.getChildByName(VO.name).y = currentTween.y;
					settings.scene.getChildByName(VO.name).z = currentTween.z;
					
				}
				
				// now do the hideMarkers
				
				var hideMarkers:Dictionary = VO.hideMarkers;
				
				settings.scene.getChildByName(VO.name).visible = true;
				
				for each(var hm:HideMarkerVO in hideMarkers)
				{
					if(hm.begin <= time && hm.end >= time)
					{
						settings.scene.getChildByName(VO.name).visible = false;
						
					}
				}
								
			} 
		
			sendNotification(Notifications.OBJECT3D_POSITION_CHANGED, camera);
		}			
	}
}